const mysql = require("mysql");
const jwt = require("jsonwebtoken");
const secret = "-_-"
const db = mysql.createPool({
    host:"8.222.222.174",
    port:"3306",
    user:"root",
    password:"123456",
    database:"ykvideo"
})
function login(req,res) {
    let sqlStr = `select count(*) from users where name="${req.body.username}" and password=md5(${req.body.password})`;
    db.query(sqlStr,(err,result)=>{
        if(!err) {
            const token = jwt.sign(username,secret,{expiresIn:"2000s"})
            res.send({
                status:"1",
                message:"登录成功",
                token:token
            })
        }
    })
}
function search(req,res) {
    let sqlStr = `select * from videos where video_title like "%${req.query.keyword}%" limit 0,20`;
    db.query(sqlStr,(err,result)=>{
        if(!err) {
            res.send({
                status:"1",
                message:"成功",
                datas:result
            })
        }
    })
}
function show(req,res) {
    let sqlStr = `select * from videos limit ${Math.ceil(Math.random()*1000)},20`;
    db.query(sqlStr,(err,result)=>{
        if(!err) {
            res.send({
                status:"1",
                message:"请求成功",
                data:result
            })
        }
    })
}
function register(req,res) {
    console.log(req.body)
    let sqlStr = `insert into users(username,password,hiredate) values("${req.body.username}",md5("${req.body.password}"),now())`
    db.query(sqlStr,(err,result)=>{
        if(!err) {
            res.send({
                status:"1",
                message:"注册成功"
            })
        }
    })
}
function logout(req,res) {
    res.send({
        status:"1",
        message:"成功",
        data:{
            content:"注销成功"
        }
    })
}
module.exports =  {
    login,register,search,show,logout
}